﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.Models;
using WebActivatorEx;

namespace Service.Helpers
{
    public sealed class CurrentGameHelper
    {
        private readonly Dictionary<int, GameState> currentGames = new Dictionary<int, GameState>();

        private static CurrentGameHelper instance;

        private CurrentGameHelper()
        {
        }

        public static CurrentGameHelper Instance
        {
            get
            {
                if (instance == null) 
                    instance = new CurrentGameHelper();
                return instance;
            }
        }

        public GameState GetCurrentGameState(int gameId)
        {
            if(!currentGames.ContainsKey(gameId))
                return null;

            GameState gameState;
            currentGames.TryGetValue(gameId, out gameState);

            return gameState;
        }

        public void AddGame(int gameId, GameState gameState)
        {
            currentGames.Add(gameId, gameState);
        }

        public void RemoveGame(int gameId)
        {
            currentGames.Remove(gameId);
        }

        public int HighestId()
        {
            if (!currentGames.Any())
                return 1;

            var highestId = currentGames.Select(g => g.Key).OrderByDescending(g => g).FirstOrDefault();

            return highestId;
        }

        public int GetPositionRow(string pos)
        {
            string row = pos.Substring(1, 1);
            switch (row)
            {
                case "1":
                    return 7;
                case "2":
                    return 6;
                case "3":
                    return 5;
                case "4":
                    return 4;
                case "5":
                    return 3;
                case "6":
                    return 2;
                case "7":
                    return 1;
                case "8":
                    return 0;
            }

            return 1;
            //return Convert.ToInt32(row) -1;
        }

        public int GetPositionCol(string pos)
        {
            string col = pos.ToLower().Substring(0, 1);
            switch (col)
            {
                case "a":
                    return 0;
                case "b":
                    return 1;
                case "c":
                    return 2;
                case "d":
                    return 3;
                case "e":
                    return 4;
                case "f":
                    return 5;
                case "g":
                    return 6;
                case "h":
                    return 7;
            }
            return 0;
        }
    }
}